package com.qf.java2106.service.impl;


import com.qf.java2106.common.ResultVO;
import com.qf.java2106.constant.MessageConstant;
import com.qf.java2106.dao.ITableDao;
import com.qf.java2106.pojos.Table;
import com.qf.java2106.service.ITableService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/30
 **/
public class TableServiceImpl implements ITableService {

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    ITableDao tableDao = (ITableDao) applicationContext.getBean("tableDao");

    /**
     * 调整餐桌状态格式
     * @param table
     * @return
     */
    private void tableStatusUtil(Table table){
        System.out.println(table.getTableStatus());
        if (0 == Integer.parseInt(table.getTableStatus())){
            table.setTableStatus("空闲");
        } else {
            table.setTableStatus("已预定");
        }
    }

    @Override
    public ResultVO findAllTable() {
        try {
            List<Table> allTable = tableDao.findAllTable();
            for (Table table : allTable) {
                tableStatusUtil(table);
                System.out.println(table);
            }
            return ResultVO.ok(MessageConstant.TABLE_QUERY_SUCCESS, allTable);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.TABLE_QUERY_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO findTableLikeKey(String keyword) {
        try {
            if(StringUtils.isEmpty(keyword)){
                keyword = "%%";
            } else {
                keyword = "%" + keyword + "%";
            }
            List<Table> tableList = tableDao.findTableLikeKey(keyword);
            for (Table table : tableList) {
                tableStatusUtil(table);
                System.out.println(table);
            }
            return ResultVO.ok(MessageConstant.TABLE_QUERY_SUCCESS,tableList);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.TABLE_QUERY_FAIL,e.getMessage());
        }
    }
}
